#include "kzfqgraphicsitemgroup.h"
#include <math.h>
#include <QString>
#include <QBrush>
#include <QPen>

kzfQGraphicsItemGroup::kzfQGraphicsItemGroup() {

}


/*
    输入2个点坐标,画出带视宽的直线,并标出实际长度
    不做逻辑,长度由外面计算并传入
*/
void kzfQGraphicsItemGroup::setLineGroup(QPointF &one, QPointF &two, double um)
{
    #define LL      10

    int lineType = 0;
    double tmpX = 0;
    double tmpY = 0;
    double angle = 0;
    double slope = 0;//斜率 与x轴

    QPointF pIn1, pIn2;//将在左边的点放到pIn1里面

    QGraphicsLineItem *lineStart;
    QGraphicsLineItem *lineReal;
    QGraphicsLineItem *lineEnd;
    QString umStr = QString::number(um, 'f', 3);
    QGraphicsSimpleTextItem *lineText = new QGraphicsSimpleTextItem(umStr);
    QLineF *rLine = new QLineF(one, two);

    QBrush brush;
    QColor *bColor = new QColor(255, 255, 0);
    bColor->setAlpha(100);
    brush.setColor(*bColor);


    QPen pen;   // 定义一个画笔，设置画笔颜色和宽度
    QPen penL;
    pen.setColor(QColor(255, 0, 0));
    pen.setWidth(1);
    penL.setColor(QColor(0, 255, 0));
    penL.setWidth(3);

    //将在左边的点放到pIn1里面
    if(one.x()==two.x()){
        //垂直直线
        slope = 0;
        lineType = 0;
        pIn1.setX(one.x());
        pIn1.setY(one.y());
        pIn2.setX(two.x());
        pIn2.setY(two.y());
    }else if(one.y()==two.y()){
        //水平直线
        slope = 0;
        lineType = 1;
        pIn1.setX(one.x());
        pIn1.setY(one.y());
        pIn2.setX(two.x());
        pIn2.setY(two.y());
    }else{
        lineType = 2;
        if(one.x()>two.x()){
            pIn1.setX(two.x());
            pIn1.setY(two.y());
            pIn2.setX(one.x());
            pIn2.setY(one.y());
        }else{
            pIn1.setX(one.x());
            pIn1.setY(one.y());
            pIn2.setX(two.x());
            pIn2.setY(two.y());
        }
        slope = (pIn2.y() - pIn1.y()) / (pIn2.x() - pIn1.x());
    }

    switch(lineType){

    case 0://垂直线
        lineStart = new QGraphicsLineItem( QLineF(QPointF(pIn1.x()-LL, pIn1.y()), QPointF(pIn1.x()+LL, pIn1.y())));
        lineReal  = new QGraphicsLineItem( QLineF(one, two));
        lineEnd   = new QGraphicsLineItem( QLineF(QPointF(pIn2.x()-LL, pIn2.y()), QPointF(pIn2.x()+LL, pIn2.y())));
        break;

    case 1://平行线
        lineStart = new QGraphicsLineItem(QLineF(QPointF(pIn1.x(), pIn1.y()-LL), QPointF(pIn1.x(), pIn1.y()+LL)));
        lineReal  = new QGraphicsLineItem(QLineF(one, two));
        lineEnd   = new QGraphicsLineItem(QLineF(QPointF(pIn2.x(), pIn2.y()-LL), QPointF(pIn2.x(), pIn2.y()+LL)));
        break;

    case 2://斜率存在不为0
        if(slope>0){
            angle = atan2(rLine->dy(), rLine->dx());
            tmpX = sin(angle)*LL;
            tmpY = cos(angle)*LL;

            lineStart = new QGraphicsLineItem(QLineF(QPointF(pIn1.x()-tmpX, pIn1.y()+tmpY), QPointF(pIn1.x()+tmpX, pIn1.y()-tmpY)));
            lineReal  = new QGraphicsLineItem(QLineF(one, two));
            lineEnd   = new QGraphicsLineItem(QLineF(QPointF(pIn2.x()-tmpX, pIn2.y()+tmpY), QPointF(pIn2.x()+tmpX, pIn2.y()-tmpY)));
        }else{
            angle = atan2(-rLine->dy(), rLine->dx());
            tmpX = sin(angle)*LL;
            tmpY = cos(angle)*LL;

            lineStart = new QGraphicsLineItem(QLineF(QPointF(pIn1.x()+tmpX, pIn1.y()+tmpY), QPointF(pIn1.x()-tmpX, pIn1.y()-tmpY)));
            lineReal  = new QGraphicsLineItem(QLineF(one, two));
            lineEnd   = new QGraphicsLineItem(QLineF(QPointF(pIn2.x()+tmpX, pIn2.y()+tmpY), QPointF(pIn2.x()-tmpX, pIn2.y()-tmpY)));
        }

        break;

    default:
        lineStart = new QGraphicsLineItem(QLineF(QPointF(pIn1.x(), pIn1.y()-LL), QPointF(pIn1.x(), pIn1.y()+LL)));
        lineReal  = new QGraphicsLineItem(QLineF(one, two));
        lineEnd   = new QGraphicsLineItem(QLineF(QPointF(pIn2.x(), pIn2.y()-LL), QPointF(pIn2.x(), pIn2.y()+LL)));
        break;
    }

    lineStart->setPen(penL);
    lineEnd->setPen(penL);
    lineReal->setPen(pen);

    lineText->setPos(pIn1.x(), (pIn1.y() - rLine->dy()/2));
    lineText->setBrush(brush);
    lineText->setPen(pen);

    this->addToGroup(lineStart);
    this->addToGroup(lineEnd);
    this->addToGroup(lineReal);
    this->addToGroup(lineText);


    // delete lineStart;
    // delete lineReal;
    // delete lineEnd;
    // delete lineText;
    // delete rLine;
}


//-------------------------------------------------------------------------------
void kzfQGraphicsItemGroup::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    // if (event->button() == Qt::LeftButton) {
        // setSelected(true);

        // qDebug()<<"circle chect one"<<isSelected();
        // qDebug()<<event->pos().x()<<event->pos().y();

        // emit signals_item_selected();
    // }

    QGraphicsItem::mousePressEvent(event);
    update();
}

void kzfQGraphicsItemGroup::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    QGraphicsItem::mouseMoveEvent(event);
    update();
}

void kzfQGraphicsItemGroup::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    QGraphicsItem::mouseReleaseEvent(event);
    update();
}
